package edu.core.testapp.client;

import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import edu.core.testapp.shared.RSAKeysString.RSACLoseKeyString;
import edu.core.testapp.shared.RSAKeysString.RSAOpenKeyString;

public class Protect02Widget extends Composite {

	private final VerticalPanel mainPanel = new VerticalPanel();
	
	private final Button btnGenerate = new Button("Generate");
	private final TextBox tbOpen1 = new TextBox();
	private final TextBox tbOpen2 = new TextBox();
	private final TextBox tbClose = new TextBox();
	private final TextArea taFrom = new TextArea();
	private final TextArea taTo = new TextArea();
	private final Button btnSwitch = new Button("Switch");
	private final Button btnEncode = new Button("Encode");
	private final Button btnDecode = new Button("Decode");
	private final Button btnToBase64 = new Button("To Base64");
	private final Label error = new Label();
	private final TextArea taFromBase64 = new TextArea();
	private final TextArea taToBase64 = new TextArea();
	
	private final Protect02ServiceAsync service = GWT.create(Protect02Service.class);
	
	public Protect02Widget() {
		
		tbOpen1.addStyleName("protect02tbOpen1");
		tbOpen2.addStyleName("protect02tbOpen2");
		tbClose.addStyleName("protect02tbClose");
		taFrom.addStyleName("protect02taFrom");
		taTo.addStyleName("protect02taTo");
		taFromBase64.addStyleName("protect02taFrom");
		taToBase64.addStyleName("protect02taTo");
		
		btnGenerate.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				service.getnerateKeys(new AsyncCallback<String[]>() {
					
					@Override
					public void onSuccess(String[] result) {
						// TODO Auto-generated method stub
						error.setText("");
						tbOpen1.setText(result[0]);
						tbOpen2.setText(result[1]);
						tbClose.setText(result[2]);
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						error.setText(caught.getMessage());
					}
				});
			}
		});
		
		btnSwitch.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				String temp = taFromBase64.getText();
				taFromBase64.setText(taToBase64.getText());
				taToBase64.setText(temp);
			}
		});
		
		btnEncode.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				taTo.setText("");
				error.setText("");
				
				service.encodeBase64(taFromBase64.getText(), new RSAOpenKeyString(tbOpen1.getText(), tbOpen2.getText()), new AsyncCallback<String>() {

					@Override
					public void onFailure(Throwable caught) {
						error.setText(caught.getMessage());
					}

					@Override
					public void onSuccess(String result) {
						taToBase64.setText(result);
						
						service.base64ToString(result, new AsyncCallback<String>() {

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub
								error.setText(caught.getMessage());
							}

							@Override
							public void onSuccess(String result) {
								// TODO Auto-generated method stub
								taTo.setText(result);
							}
						});
					}
				});
			}
		});
		
		btnDecode.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				taTo.setText("");
				error.setText("");
				service.decodeBase64(taFromBase64.getText(), composeOpenKey(), composeCloseKey(), new AsyncCallback<String>() {

					@Override
					public void onFailure(Throwable caught) {
						error.setText(caught.getMessage());
					}

					@Override
					public void onSuccess(String result) {
						taToBase64.setText(result);
						
						service.base64ToString(result, new AsyncCallback<String>() {

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub
								error.setText(caught.getMessage());
							}

							@Override
							public void onSuccess(String result) {
								// TODO Auto-generated method stub
								taTo.setText(result);
							}
						});
					}
				});
			}
		});
		
		btnToBase64.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				service.stringToBase64(taFrom.getText(), new AsyncCallback<String>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						error.setText(caught.getMessage());
					}

					@Override
					public void onSuccess(String result) {
						// TODO Auto-generated method stub
						taFromBase64.setText(result);
					}
					
				});
			}
		});
		
		FlexTable tab = new FlexTable();
		tab.setWidget(0,  0, new Label("Open"));
		tab.setWidget(0, 1, tbOpen1);
		tab.setWidget(1, 1, tbOpen2);
		tab.setWidget(2, 0, new Label("Close"));
		tab.setWidget(2, 1, tbClose);
		
		mainPanel.add(error);
		mainPanel.add(tab);
		mainPanel.add(btnGenerate);
		mainPanel.add(taFrom);
		
		tab = new FlexTable();
		tab.setWidget(0, 0, btnSwitch);
		tab.setWidget(0, 1, btnEncode);
		tab.setWidget(0, 2, btnDecode);
		tab.setWidget(0, 3, btnToBase64);
		mainPanel.add(tab);
		
		tab = new FlexTable();
		tab.setWidget(0, 0, taFrom);
		tab.setWidget(0, 1, taFromBase64);
		tab.setWidget(1, 0, taTo);
		tab.setWidget(1, 1, taToBase64);
		mainPanel.add(tab);
		
		initWidget(mainPanel);
		
	}
	
	RSAOpenKeyString composeOpenKey() {
		return new RSAOpenKeyString(tbOpen1.getText(), tbOpen2.getText());
	}
	
	RSACLoseKeyString composeCloseKey() {
		return new RSACLoseKeyString(tbClose.getText());
	}
}
